package com.weixin.program.utils;

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2021年03⽉11⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
public class Excel2ImageUtil {

    /**
     * Excel文件转图片
     *
     * @param excelPath
     */
    public static void Excel2Image(String excelPath, String imagepath) {
        File file = new File(excelPath);
        if (!file.exists()) {
            log.error("Excel文件不存在,excelPath={}", excelPath);
            return;
        }
        File file2 = new File(imagepath);
        if (!file2.exists()) {
            file2.mkdirs();
        }

        //加载Excel工作表
        Workbook wb = new Workbook();
        wb.loadFromFile(excelPath);

        //获取工作表
        Worksheet sheet = null;
        try {
            sheet = wb.getWorksheets().get(0);
            //调用方法将Excel工作表保存为图片
            sheet.saveToImage(imagepath);
            log.info("Excel生成图片成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
